package vm.tools;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Java 死锁demo
 * jcmd 6940 Thread.print，可查看线程栈，会提示出死锁存在
 **/
public class MyDeathLock {
        private static Lock lock1 = new ReentrantLock();
        private static Lock lock2 = new ReentrantLock();

        public static void deathLock() {
            Thread t1 = new Thread(() -> {
                try {
                    lock1.lock();
                    System.out.println(Thread.currentThread().getName() + " get the lock1");
                    // 这个sleep保证了都能得到一个锁
                    Thread.sleep(1000);
                    lock2.lock();
                    System.out.println(Thread.currentThread().getName() + " get the lock2");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            Thread t2 = new Thread(() -> {
                try {
                    lock2.lock();
                    System.out.println(Thread.currentThread().getName() + " get the lock2");
                    Thread.sleep(1000);
                    lock1.lock();
                    System.out.println(Thread.currentThread().getName() + " get the lock1");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            //设置线程名字，方便分析堆栈信息
            t1.setName("mythread-jay");
            t2.setName("mythread-tianluo");
            t1.start();
            t2.start();
        }
        public static void main(String[] args) {
            deathLock();
        }


}
